package cn.usian.service.impl;

import cn.usian.common.R;
import cn.usian.entity.Employee;

import cn.usian.exception.NameException;
import cn.usian.mapper.EmployeeMapper;
import cn.usian.service.EmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
public class EmployeeImpl implements EmployeeService {
    @Resource
    private EmployeeMapper employeeMapper;
    @Override
    public R<Employee> login(Employee employee) {
        //检查用户输入的用户名是否存在
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee selectOne = employeeMapper.selectOne(queryWrapper);
        //判断密码是否存在
        if (selectOne==null){
            return R.error("不在");
        }
        String hex = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        //判断用户输入的密码进行md5转换
        if (hex.equals(selectOne.getPassword())){
            return R.error("密码错误");
        }
        //判定状态是否存在
        if (selectOne.getStatus()==0){
            return R.error("账号禁用");
        }
        return R.success(selectOne);
    }

    @Override
    public R<Page> page(int page, int pageSize, String name) {
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,Employee::getName,name);
        Page<Employee> page1 = new Page<>(page,2);
        Page<Employee> employeePage = employeeMapper.selectPage(page1, queryWrapper);
        return R.success(employeePage);
    }

    @Override
    public void save(Employee employee) {
        //判断当前用户是否存在
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //添加用户的查询条件
        queryWrapper.eq(Employee::getUsername,employee.getUsername());

        Employee employee1=employeeMapper.selectOne(queryWrapper);
        if (employee1!=null){
            throw new NameException(employee.getUsername());
        }
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()).toString());
        employeeMapper.insert(employee);
    }

    @Override
    public void updateEmp(Employee employee) {
        employee.setUpdateTime(LocalDateTime.now());
        LambdaUpdateWrapper<Employee> updateWrapper = new LambdaUpdateWrapper<>();
        if (employee.getName()!=null&&employee.equals("")){
            updateWrapper.set(Employee::getName,employee.getName());
        }
        if (employee.getUsername()!=null&&employee.equals("")){
            updateWrapper.set(Employee::getUsername,employee.getUsername());
        }

        if (employee.getPhone()!=null&&employee.equals("")){
            updateWrapper.set(Employee::getPhone,employee.getPhone());
        }

        if (employee.getSex()!=null&&employee.equals("")){
            updateWrapper.set(Employee::getSex,employee.getSex());
        }

        if (employee.getIdNumber()!=null&&employee.equals("")){
            updateWrapper.set(Employee::getIdNumber,employee.getIdNumber());
        }

        if (employee.getStatus()!=null&&employee.equals("")){
            updateWrapper.set(Employee::getStatus,employee.getStatus());
        }
        employeeMapper.updateById(employee);

    }

    @Override
    public R findByid(Long id) {
        return R.success(employeeMapper.selectById(id));
    }


}
